﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using cludg.Web.Core.ViewModels.Extensions;
using cludg.Web.RateContentModule.Services;
using cludg.Web.ViewModels;
using cludg.Web.Core.ViewModels;

namespace cludg.Web.RateContentModule.Extensions
{
	public class ViewModelExtensionProvider : IViewModelExtensionProvider
	{
		protected readonly RateService RateService;
		protected readonly cludg.Services.Security.IAuthorization Authorization;

		public ViewModelExtensionProvider(RateService rateService,
			cludg.Services.Security.IAuthorization authorization)
		{
			RateService = rateService;
			Authorization = authorization;
		}

		protected void InitStoryDetailsModel(StoryDetailsModel model)
		{
			int posRate;
			int negRate;
			RateService.GetStorySummary(model.Story, out posRate, out negRate);
			model["PositiveRate"] = posRate;
			model["NegativeRate"] = -negRate;
			if (!Authorization.HasRights(model.SecurityInfo.CurrentUser.User, "RateStory", model.Story))
			{
				model["CanUserRate"] = false;
			}
			else
			{
				model["CanUserRate"] = true;
				var rate = RateService.Get(model.SecurityInfo.CurrentUser.User, model.Story);
				model["CurrentUserRate"] = (rate == null) ? 0 : rate.Rate;
			}
		}

		protected void InitTopStories(BaseModel model)
		{
			model["Top"] = RateService.TopRatedStories(10);
		}

		public ViewModelExtension[] GetExtensions()
		{
			return new ViewModelExtension[]
			{
				new ViewModelActionExtension<StoryDetailsModel>(InitStoryDetailsModel),
				new ViewModelActionExtension<BaseModel>(InitTopStories)
			};
		}
	}
}